angular.module('baisonWaitUpLoad', ['baisonDb', 'baisonService'])
        //获取未上传信息
        .factory('waitUpLoad', ['$q', '$rootScope', '$location', 'dbSelect', 'apiService', 'dbUpdate', 'panDian', 'ReceiptRecord', 'VIP', function ($q, $rootScope, $location, dbSelect, apiService, dbUpdate, panDian, ReceiptRecord, VIP) {
                var receipt_record_get_wait_upload_group_list = function (callback) {
                    return ReceiptRecord.get_list_local_not_upload();
                };
                var vip_get_wait_upload_group_list = function (callback) {
                    return VIP.get_list_local_not_upload();

                };
                var take_stock_get_wait_upload_group_list = function (callback) {
                    return panDian.get_list_local_not_upload();
                };
                //获取小票明细
                var receipt_detail_by_record_code_local = function (record_code, callback) {
                    var sql = 'select * from PosReceiptDetail where record_code="' + record_code + '"';
                    return dbSelect('YsPos', sql);

                };
                //获取结算方式
                var receipt_settlement_by_record_code_local = function (record_code, callback) {
                    var sql = 'select * from PosReceiptSettlement where record_code="' + record_code + '"';
                    dbSelect('YsPos', sql).then(function (ret) {
                        callback(ret);
                    }).catch(function (err) {
                        baison_app.api.alert({msg: err});
                    });
                };
                var receipt_promotion_by_record_code_local = function(record_code,callback){
                    var sql = 'select * from PosReceiptPromotion where record_code="' + record_code + '"';
                    dbSelect('YsPos', sql).then(function (ret) {
                        callback(ret);
                    }).catch(function (err) {
                        baison_app.api.alert({msg: err});
                    });
                };
                //上传离线小票
                var receipt_record_loacl_to_api = function (record_data) {
                    var deferred = {};
                    var promise = {};
                    var record_list = record_data;
                    var offline = false;
                    var arr_deferred = {};
                    var arr_promise = {};

                    var execWaitUp = function(i){
                        n = record_list[i];
                        if(n == undefined){
                            arr_deferred[i].resolve('false');
                            return;
                        }
						ReceiptRecord.get_one_local(n.record_code).then(function(data){
							//console.log(data);
							data.record[0].integral = 0;
							data.record_detail.forEach(function(detail,dkey){
								
                                detail.guide_user_code = detail.daogou_code;
								if(detail.points && detail.points != 0){
									data.record[0].integral = record_data.record[0].integral - detail.points;
								}
                            });
							data.record[0].is_add_person = n.trade_user_code;
                            data.record[0].sell_time = data.record[0].record_time;
							data.record[0].total_money = data.record.final_money;
							data.record_code = n.record_code;
							data.device_sn = baison_app.api.device_sn;
							apiService('pos/pos/upload_xiaopiao', data).then(function (ret) {
								console.log(ret);
								if (ret.data.status == 1) {
									dbUpdate('PosReceiptRecord', {'comment': ret.data.message, 'is_upload': 1}, {'record_code': n.record_code}, function () {
									});
									arr_deferred[i].resolve('true');
									i++;
									execWaitUp(i);

								}else if(ret.data.status == '-2'){
									dbUpdate('PosReceiptRecord', {'comment': ret.data.message, 'is_upload': 1}, {'record_code': n.record_code}, function (ret) {		
									});
									arr_deferred[i].resolve('false');
									i++;
									execWaitUp(i);
								}else {
									
									dbUpdate('PosReceiptRecord', {'comment': 'fail:' + ret.data.message}, {'record_code': n.record_code}, function () {
									});
									arr_deferred[i].resolve('false');
									i++;
									execWaitUp(i);

								}
							}).catch(function(err){
								baison_app.api.alert({title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_NETWORK_ABNORMAL});
                                baison_app.api.hideProgress();
								arr_deferred[i].resolve('false');
								i++;
								execWaitUp(i);
							});
						});

                    };

                    for (var j = 0; j <= record_list.length; j++) {
                        arr_deferred[j] = $q.defer();
                        arr_promise[j] = arr_deferred[j].promise;
                    }
                    var index = 0;
                    execWaitUp(index);
                    return $q.all(arr_promise);

                };
                //上传离线会员
                var vip_loacl_to_api = function (vip_data) {

                    var deferred = {};
                    var promise = {};
                    var offline = false;
                    var record_list = vip_data;

                    var arr_deferred = {};
                    var arr_promise = {};
                    var execVipWaitUp = function(i){
                        n = record_list[i];
                        if(n == undefined){
                             arr_deferred[i].resolve('false');
                             return;
                        }
                        var data = {
                            'customer_code': n.customer_code,
                            'customer_name': n.customer_name,
                            'customer_tel': n.customer_tel,
                            'vip_code': n.vip_code,
                            'customer_sex': n.customer_sex,
                            'email': n.email,
                            'address': (n.address == "undefined") ? '' : n.address,
                            'birthday': n.birthday,
                            'shop_code':$rootScope.current_shop.ShopCode,
                            'user_code':$rootScope.user.UserCode,
                            'VIP_category':(n.VIP_category == "undefined")? 0 :n.VIP_category,
                            'province': (n.province == "undefined") ? '' : n.province,
                            'city': (n.city == "undefined") ? '' : n.city,
                            'district': (n.county == "undefined") ? '' : n.county,
                            'skin_type': n.skin,
                            'weixin_id': n.weixin,
                            'qq': (n.qq == "undefined") ? '' : n.qq,
                            'daogouyuan': n.is_add_person
                        }
                        
                        apiService('crm/vip/add_customer', data).then(function (ret) {
                            if (ret.data.status == 1) {
                                dbUpdate('crm_customer', {'is_upload': 1, 'comment': ret.data.message}, {'customer_code': n.customer_code}, function (ret) {

                                });
                                arr_deferred[i].resolve('true');
                                i++;
                                execVipWaitUp(i);
                            } else {
                                dbUpdate('crm_customer', {'comment': 'fail:' + ret.data.message}, {'customer_code': n.customer_code}, function (ret) {

                                });
                                arr_deferred[i].resolve('false');
                                i++;
                                execVipWaitUp(i);
                            }

                        }).catch(function (err) {
                            baison_app.api.alert({title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_NETWORK_ABNORMAL});
                            baison_app.api.hideProgress();
                            
                        });
                    };
                    for (var j = 0; j <= record_list.length; j++) {
                        arr_deferred[j] = $q.defer();
                        arr_promise[j] = arr_deferred[j].promise;
                    }
                    var index = 0;
                    execVipWaitUp(index);
                    return $q.all(arr_promise);
                };
                return {
                    receipt_record_get_wait_upload_group_list: receipt_record_get_wait_upload_group_list,
                    vip_get_wait_upload_group_list: vip_get_wait_upload_group_list,
                    take_stock_get_wait_upload_group_list: take_stock_get_wait_upload_group_list,
                    receipt_record_loacl_to_api: receipt_record_loacl_to_api,
                    vip_loacl_to_api: vip_loacl_to_api
                };
            }]);